/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.subscriber.processor;

import lk.mrt.cse.wihidum.client.*;
import org.apache.axiom.om.*;
import org.apache.axiom.soap.*;
import org.apache.axis2.addressing.*;
import org.apache.log4j.*;

import javax.xml.namespace.*;

/**
 * Process subscription messages
 */
public class SubscriptionProcessor implements Processor {
    private static final Logger logger = Logger.getLogger(SubscriptionProcessor.class);

    /**
     * @see lk.mrt.cse.wihidum.subscriber.processor.Processor#processMessage(org.apache.axiom.om.OMElement)
     */
    public OMElement processMessage(OMElement requestElement) {
        try {
            //logger.info ("Processing subscription message");

            OMElement subscriptionIDElement = requestElement.getFirstChildWithName(new QName("subscriptionID"));
            OMElement brokerAddressElement = requestElement.getFirstChildWithName(new QName("brokerAddress"));
            OMElement adminAddressElement = requestElement.getFirstChildWithName(new QName("adminAddress"));
            OMElement listenerAddressElement = requestElement.getFirstChildWithName(new QName("listenerAddress"));
            OMElement subscriberAddressElement = requestElement.getFirstChildWithName(new QName("subscriberAddress"));
            OMElement topicElement = requestElement.getFirstChildWithName(new QName("topic"));

            SubscriptionClientBean clientBean = new SubscriptionClientBean();

            clientBean.setSubscriptionID(subscriptionIDElement.getText());
            clientBean.setBrokerAddress(new EndpointReference(brokerAddressElement.getText()));
            clientBean.setAdminAddress(new EndpointReference(adminAddressElement.getText()));
            clientBean.setListenerAddress(new EndpointReference(listenerAddressElement.getText()));
            clientBean.setSubscriberAddress(new EndpointReference(subscriberAddressElement.getText()));
            clientBean.setTopic(topicElement.getText());

            SubscriptionClient subscriptionClient = new SubscriptionClient(clientBean);

            String response = subscriptionClient.subscribe();
            //logger.info (response);

            return createSubscriptionResponseElement(response);
        }
        catch (OMException ex) {
            logger.error("Error in processing subscription message", ex);
            return createSubscriptionFaultResponseElement();
        }
    }

    /**
     * Create response for a valid subscription
     *
     * @param response Response message
     * @return Response element
     */
    private OMElement createSubscriptionResponseElement(String response) {
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();

        OMElement responseElement = factory.createOMElement("SubscribeResponse", null);
        //OMElement referenceParameters = factory.createOMElement ("ReferenceParameters", null);
        //OMElement responseTextElement = factory.createOMElement ("Response", null);

        //responseTextElement.setText (response);
        //referenceParameters.addChild (responseTextElement);
        responseElement.setText(response);

        return responseElement;
    }

    /**
     * Create response for an invalid subscription
     *
     * @return Response element
     */
    private OMElement createSubscriptionFaultResponseElement() {
        return null;
    }
}
